#include <intrins.h>
#include "util.h"
#include "uart.h"
#include <stdio.h>

void die()
{
	for (;;)
		;
}

void show_buffer(unsigned char *buffer, unsigned char size)
{
	unsigned char i;

	for (i = 0; i < size; i++)
	{
		printf("%02X ", (int)buffer[i]);
	}
	printf("\r\n");
}

void buffer_left_shift(unsigned char *pbuffer, unsigned char length, unsigned char offset)
{
	unsigned char i;

	for (i = offset; i < length; i++)
	{
		pbuffer[i - offset] = pbuffer[i];
	}
}

void buffer_right_shift(unsigned char *pbuffer, unsigned char length, unsigned char offset)
{
	unsigned char i;

	for (i = length; i > 0; i--)
	{
		pbuffer[i - 1 + offset] = pbuffer[i - 1];
	}
}

unsigned short crc16(unsigned char *pbuffer, unsigned char length)
{
	unsigned short crc = 0;
	unsigned char i;

	for (i = 0; i < length; i++)
	{
		crc += pbuffer[i];
	}
	return crc;
}

unsigned char hex_to_binary(unsigned char ch)
{
	if (ch >= '0' && ch <= '9')
		return ch - '0';
	else if (ch >= 'a' && ch <= 'z')
		return ch - 'a' + 10;
	else if (ch >= 'A' && ch <= 'Z')
		return ch - 'A' + 10;
	return 0;
}
